/*
 * @Author: your name
 * @Date: 2020-07-31 19:46:09
 * @LastEditTime: 2020-08-01 01:32:37
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: /Projects/linux/Program/学生管理系统V2.0/功能/read.c
 */ 
#include "read.h"
// 字符串包含
int is_string_contain(const char *str1, const char *str2)
{
    for (int i = 0, j = 0; i < strlen(str1); i++)
    {
        if (*(str1 + i) == *(str2 + j))
        {
            j++;
        }
        if (*(str2 + j) == '\0')
        {
            return 1;
        }
    }
	return 0;
}

void read_student_data_from_file(STUDENT *head, FILE *fp)
{
    if (fp == NULL || head == NULL)
    {
		return;
	}
    char buf[100] = {'\0'};

	char number[50] = {'\0'};
	char name[50] = {'\0'};
    char phone[50] = {'\0'};
	int Chinese;
    int Math;
    int English;
    STUDENT *node;

    while (fgets(buf, sizeof(buf), fp) != NULL)
    {
        if (is_string_contain(buf, "number"))
        {
            // printf("1buf:%s", buf);
	        sscanf(buf, " <number>%[^<]</number>", number);
            // printf("2buf:%s", number);

            fgets(buf, sizeof(buf), fp);
            sscanf(buf, " <name>%[^<]</name>", name);
            // printf("2%s", name);
       
            fgets(buf, sizeof(buf), fp);
            sscanf(buf, " <phone>%[^<]</phone>", phone);
            // printf("3%s", phone);
 
            node = creat_node_by_basic_information(number, name, phone);
            insert_node(head, node);
        }
        if (is_string_contain(buf, "Chinese"))
        {
            sscanf(buf, " <Chinese>%d</Chinese>", &Chinese);
            // printf("%d", Chinese);
            fgets(buf, sizeof(buf), fp);
            sscanf(buf, " <Math>%d</Math>", &Math);
            // printf("%d", Math);
            fgets(buf, sizeof(buf), fp);
            sscanf(buf, " <English>%d</English>", &English);
            // printf("%d", English);
            insert_stuNode_score(head, name, Chinese, Math, English);
        }
    }
}
